package cn.edu.njnu.service;

import cn.edu.njnu.dto.AdviceDto;
import cn.edu.njnu.mapper.AdviceMapper;
import cn.edu.njnu.pojo.Advice;
import cn.edu.njnu.pojo.Result;
import cn.edu.njnu.pojo.ResultFactory;
import cn.edu.njnu.vo.AdvicePageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.beans.binding.ObjectExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class AdviceService {
    @Autowired
    private AdviceMapper adviceMapper;
    public Result addAdvice(Map<String, Object> fieldMap){
        String adviceStr = (String)fieldMap.get("adviceStr");
        if (adviceStr==null){
            return ResultFactory.buildFailResult("建议内容不能为空");
        }
        adviceMapper.insertAdvice(adviceStr, LocalDateTime.now());
        return ResultFactory.buildSuccessResult("意见提交成功",null);
    }
    public Result getAdvice(AdviceDto adviceDto){
        PageHelper.startPage(adviceDto.getPageNum(), adviceDto.getPageSize());
        List<Advice> adviceList = adviceMapper.queryAdvice();
        PageInfo<Advice> advicePageInfo = new PageInfo<>(adviceList);
        return ResultFactory.buildSuccessResult("查询成功",new AdvicePageVo()
                .setPages(advicePageInfo.getPages())
                .setTotal(advicePageInfo.getTotal())
                .setAdviceList(advicePageInfo.getList())
        );
    }
}
